package com.ll.domain.user.event.consumer;

import com.ll.domain.pk.dto.PkDTO;
import com.ll.domain.pk.event.producer.AddPkEvent;
import com.ll.domain.user.base.UserBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * pk 记录添加
 * @author WuHen
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AddPkEventToUserListener implements ApplicationListener<AddPkEvent> {

    private final UserBaseService userBaseService;

    @Async
    @Override
    public void onApplicationEvent(AddPkEvent addPkEvent) {
        PkDTO pkDTO = addPkEvent.getPkDTO();
        log.info("接收到 PK信息添加事件 id ：{}", pkDTO.getId());

        userBaseService.addIntegral(pkDTO.getSourceId(), -pkDTO.getIntegral());

        userBaseService.addIntegral(pkDTO.getTargetId(), -pkDTO.getIntegral());

        log.info("接收到 PK信息添加事件 id ：{} 处理成功", pkDTO.getId());
    }
}
